using UnityEngine;

public class PoolObject : MonoBehaviour, IPoolObject
{
    private ICachePool _cachePool;

    protected void Awake()
    {
        //默认打开
        TurnOn();
    }

    public void TurnOn()
    {
        if (IsTurnOn) return;
        IsTurnOn = true;
        OnTurnOnHandle();
    }

    public void TurnOff()
    {
        IsTurnOn = false;
        OnTurnOffHandle();
    }

    public void SetObject(GameObject target)
    {
    }

    public void SetOwner(ICachePool pool)
    {
        _cachePool = pool;
    }

    public void SetParent(Transform value)
    {
        if (transform.parent != value)
        {
            transform.SetParent(value);
        }
    }

    public void DestroySelf()
    {
        Destroy(gameObject);
    }

    virtual protected void OnDestroy()
    {
        if (_cachePool != null)
        {
            _cachePool.RemoveBy(this);
            _cachePool = null;
        }
        else
        {
            CachePoolManager.Remove(this);
        }
    }

    /// <summary>
    /// 被回收
    /// </summary>
     virtual public void Recycle()
    {
        if (_cachePool != null) _cachePool.RecycleBy(this);
        else
        {
            CachePoolManager.RecycleOne(this);
        }
    }


    public bool IsTurnOn { get; set; }
    virtual protected void OnTurnOnHandle()
    {
        gameObject.SetActive(true);
    }
    virtual protected void OnTurnOffHandle()
    {
        gameObject.SetActive(false);
    }
}